﻿package mortal.game.view.forging.renderer
{
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.forging.*;
    import mortal.game.view.forging.util.*;
    import mortal.game.view.palyer.*;
    import mortal.mvc.core.*;

    public class GemPackItemCellRenderer extends GCellRenderer
    {
        private var _item:BaseItem;
        private var _itemData:ItemData;

        public function GemPackItemCellRenderer()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._item = UICompomentPool.getUICompoment(BaseItem);
            this._item.setItemStyle(ItemStyleConst.Small);
            this._item.isDragAble = false;
            this.addChild(this._item);
            this._item.mouseEnabled = true;
            this._item.doubleClickEnabled = true;
            this._item.buttonMode = true;
            this._item.configEventListener(MouseEvent.DOUBLE_CLICK, this.onDBClickHandler);
            this._item.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            var _loc_2:PlayerEquipItem = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            super.data = param1;
            if (param1 is ItemData)
            {
                this._itemData = param1 as ItemData;
                this._item.itemData = param1 as ItemData;
                this._item.isBind = Boolean(this._itemData.bind);
                _loc_2 = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
                if (_loc_2 && _loc_2.itemData)
                {
                    _loc_3 = _loc_2.itemData.itemInfo.type;
                    _loc_4 = EquipJewelMatchConfig.instance.getInfoByType(_loc_3).jeweltype;
                    if (this._itemData.itemInfo.type == _loc_4)
                    {
                        this._item.canNotUse = false;
                    }
                    else
                    {
                        this._item.canNotUse = true;
                    }
                }
                else
                {
                    this._item.canNotUse = false;
                }
            }
            else
            {
                this._item.itemData = null;
                this._item.canNotUse = false;
            }
            return;
        }// end function

        private function onDBClickHandler(event:MouseEvent) : void
        {
            var _loc_2:PlayerEquipItem = null;
            if (this._itemData && !this._item.canNotUse)
            {
                _loc_2 = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
                if (!(_loc_2 && _loc_2.itemData))
                {
                    this.selEquipByGem();
                    return;
                }
                if (this._itemData.itemInfo.category == EStuff._EStuffJewel)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingEmbedGem, this._itemData));
                }
            }
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            if (this._itemData && this._item.canNotUse || _loc_2 == null)
            {
                this.selEquipByGem();
            }
            return;
        }// end function

        private function selEquipByGem() : void
        {
            var _loc_4:PlayerEquipItem = null;
            var _loc_5:String = null;
            var _loc_6:String = null;
            var _loc_7:String = null;
            var _loc_1:* = this._itemData.itemInfo.type;
            var _loc_2:* = EquipJewelMatchConfig.instance.getEquipTypeByJewelType(_loc_1);
            var _loc_3:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.getEquipByType(_loc_2[0]);
            if (_loc_2.length == 2)
            {
                if (!_loc_3.itemData || !EquipmentUtil.isEmbedAvailable(_loc_3.itemData))
                {
                    _loc_3 = (GameController.forging.view as ForgingModule).equipDisplaySpr.getEquipByType(_loc_2[1]);
                }
            }
            if (_loc_3.itemData)
            {
                _loc_4 = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
                if (_loc_4)
                {
                    _loc_4.isSelect = false;
                }
                (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip = _loc_3;
                _loc_3.setSelEffect(true);
                GemUtil.isNeedUpdate = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.AddForgingEquip, _loc_3));
            }
            else if (_loc_2.length > 0)
            {
                _loc_5 = GameDefConfig.instance.getEquipName(_loc_2[0]);
                if (_loc_2.length == 2)
                {
                    _loc_6 = GameDefConfig.instance.getEquipName(_loc_2[1]);
                }
                _loc_7 = "";
                if (_loc_6 == null)
                {
                    _loc_7 = Language.getStringByParam(41052, _loc_5);
                }
                else
                {
                    _loc_7 = Language.getStringByParam(41053, _loc_5, _loc_6);
                }
                MsgManager.showRollTipsMsg(_loc_7);
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._item.dispose();
            this._item = null;
            this._itemData = null;
            return;
        }// end function

    }
}
